package com.hut.bms.controller;

import com.hut.bms.model.BookInfo;
import com.hut.bms.service.UpdateBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RequestMapping("/BMS")
@RestController
public class UpdateBookController {
    @Autowired
    private UpdateBookService updateBookService;
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        //打印从前端参数传入到后端的日志
        log.info("添加图书, 接收到参数bookInfo: {}", bookInfo);
        //校验参数
        if (bookInfo.getId() == null
                ||!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
            return "输入参数不合法";
        }
        //更新图书
        try {
            Integer line =  updateBookService.updateBookInfo(bookInfo);
            if(line > 0) {
                return "";
            }
        }catch (Exception e) {
            log.info("更新图书失败，e:",e);
        }
        return "更新失败";
    }
}
